use sea_orm_migration::prelude::*;

use entity::{resource, role_resource};

use crate::sea_orm::{ActiveModelTrait, NotSet};
use crate::sea_orm::ActiveValue::Set;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        let db = manager.get_connection();
        resource::ActiveModel {
            id: Set(60),
            parent_id: Set(1),
            resource_name: Set(Some("游戏角色".to_string())),
            resource_code: Set(Some("game_role_manager".to_string())),
            resource_type: Set(Some(1)),
            resource_root: Set(Some(true)),
            resource_action: Set(Some(false)),
            order_number: Set(Some(0)),
            url: Set(Some("accountsManager".to_string())),
            api_path: NotSet,
            api_http_method: Default::default(),
            role: NotSet,
            status: Set(Some(true)),
            icon: NotSet,
            resource_desc: Set(Some("游戏角色管理菜单".to_string())),
        }
        .insert(db)
        .await?;
        resource::ActiveModel {
            id: Set(61),
            parent_id: Set(60),
            resource_name: Set(Some("游戏角色详情".to_string())),
            resource_code: Set(Some("api_game_role_info".to_string())),
            resource_type: Set(Some(2)),
            resource_root: Set(Some(false)),
            resource_action: Set(Some(true)),
            order_number: Set(Some(0)),
            url: Default::default(),
            api_path: Set(Some("/api/game_role/info/".to_string())),
            api_http_method: Set(Some("GET".to_string())),
            role: NotSet,
            status: Set(Some(true)),
            icon: NotSet,
            resource_desc: Set(Some("游戏角色详情接口".to_string())),
        }
        .insert(db)
        .await?;
        resource::ActiveModel {
            id: Set(62),
            parent_id: Set(60),
            resource_name: Set(Some("游戏角色列表".to_string())),
            resource_code: Set(Some("api_game_role_list".to_string())),
            resource_type: Set(Some(2)),
            resource_root: Set(Some(false)),
            resource_action: Set(Some(true)),
            order_number: Set(Some(0)),
            url: Default::default(),
            api_path: Set(Some("/api/game_role/list".to_string())),
            api_http_method: Set(Some("GET".to_string())),
            role: NotSet,
            status: Set(Some(true)),
            icon: NotSet,
            resource_desc: Set(Some("游戏角色列表接口".to_string())),
        }
        .insert(db)
        .await?;
        resource::ActiveModel {
            id: Set(63),
            parent_id: Set(60),
            resource_name: Set(Some("游戏角色分页查询".to_string())),
            resource_code: Set(Some("api_game_role_page".to_string())),
            resource_type: Set(Some(2)),
            resource_root: Set(Some(false)),
            resource_action: Set(Some(true)),
            order_number: Set(Some(0)),
            url: Default::default(),
            api_path: Set(Some("/api/game_role/page".to_string())),
            api_http_method: Set(Some("POST".to_string())),
            role: NotSet,
            status: Set(Some(true)),
            icon: NotSet,
            resource_desc: Set(Some("游戏角色分页查询接口".to_string())),
        }
        .insert(db)
        .await?;
        resource::ActiveModel {
            id: Set(64),
            parent_id: Set(60),
            resource_name: Set(Some("修改游戏角色".to_string())),
            resource_code: Set(Some("api_game_role_update".to_string())),
            resource_type: Set(Some(2)),
            resource_root: Set(Some(false)),
            resource_action: Set(Some(true)),
            order_number: Set(Some(0)),
            url: Default::default(),
            api_path: Set(Some("/api/game_role/update".to_string())),
            api_http_method: Set(Some("POST".to_string())),
            role: NotSet,
            status: Set(Some(true)),
            icon: NotSet,
            resource_desc: Set(Some("修改游戏角色接口".to_string())),
        }
        .insert(db)
        .await?;
        resource::ActiveModel {
            id: Set(65),
            parent_id: Set(60),
            resource_name: Set(Some("删除游戏角色".to_string())),
            resource_code: Set(Some("api_game_role_delete".to_string())),
            resource_type: Set(Some(2)),
            resource_root: Set(Some(false)),
            resource_action: Set(Some(true)),
            order_number: Set(Some(0)),
            url: Default::default(),
            api_path: Set(Some("/api/game_role/delete".to_string())),
            api_http_method: Set(Some("GET".to_string())),
            role: NotSet,
            status: Set(Some(true)),
            icon: NotSet,
            resource_desc: Set(Some("删除游戏角色接口".to_string())),
        }
        .insert(db)
        .await?;
        for id in 60..66 {
            role_resource::ActiveModel {
                id: NotSet,
                role_id: Set(1),
                resource_id: Set(id),
            }
            .insert(db)
            .await?;
        }
        Ok(())
    }

    async fn down(&self, _manager: &SchemaManager) -> Result<(), DbErr> {
        Ok(())
    }
}
